import { _decorator, Component, Node } from 'cc';
import { ComponentBase } from './ComponentBase';
import { Message, MessageType } from './Message';
import { MessageCenter } from './MessageCenter';
const { ccclass, property } = _decorator;

@ccclass('ManagerBase')
export class ManagerBase extends ComponentBase {

    children: Array<ComponentBase> = [];

    onRegister(child:ComponentBase){
        this.children.push(child);
    }
    removeRegister(child:ComponentBase){
        var index = this.children.indexOf(child);
        if(index != -1){
            this.children.splice(index,1);
        }
    }


    onLoad() {
        this._register();
    }

    _register() {
        //注册管理类
        MessageCenter.addManager(this);

    }
    _removeRgister() {
        //注销管理类
        MessageCenter.removeManager(this);
    }


    getType(): MessageType {
        return MessageType.TypeUI;
    }

    onReceiveMessage(message: Message) {
        super.onReceiveMessage(message);

        if (message.type != this.getType()) {
            return;
        }
        for (let child of this.children) {
            child.onReceiveMessage(message);
        }
    }
}

